import Vue from 'vue'
import Vuex from 'vuex'
import config from '@/config'
import util from '@/common/js/util'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		tabCurrentIndex: 0,
		tabPreviousIndex: 0,
		tabList: config.tabList,
		theme: '',
		user: null
	},
	actions: {
		switchTab({
			state
		}, tabName) {
			var tab = state.tabList.filter(x => x.name === tabName)[0]
			state.tabPreviousIndex = state.tabCurrentIndex
			state.tabCurrentIndex = state.tabList.indexOf(tab)
			state.tabList.forEach(x => x.selected = false)
			tab.selected = true
			tab.load = true
		},
		switchTheme({
			state
		}, themeName) {
			state.theme = themeName
		}
	},
	mutations: {
		login(state, user) {
			state.user = user
			util.setStorage('user', user)
		},
		logout(state) {
			util.removeStorage('user')
			state.user = null
		}
	}
})

export default store
